package com.chunuo.configuration.factory;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * jdbc
 */
public class JdbcUtils {

	private static DataSource ds = null;
	
	public static void create(Properties properties){

        try {
            //创建druid连接池，将配置properties直接导入
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

	/*
	    创建连接
	 */
	public static Connection getConnection() {
		Connection connection = null;
		try {
			 connection = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}

	/*
	    删除连接，返回连接池
	 */
	public static void removeConnection(Connection connection) {
		if(connection!=null) {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
